
/*
 * @Description: 
 * @Autor: 曹敬涛
 * @Date: 2021-08-04 15:45:18
 * @LastEditors: 商晓彬
 * @LastEditTime: 2021-11-17 10:47:21
 */
import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Input, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { MessageService } from '@abp/message/message.service';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { QualityManageServiceProxy, QualitySeverityDescDto, QualityItemInfoDto, QualityTypeDescDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
@Component({
  selector: 'app-quality',
  templateUrl: './quality.component.html',
  styleUrls: ['./quality.component.less']
})
export class QualityComponent extends AppComponentBase {
  @ViewChild('paginator') paginator: Paginator;
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('typePaginator') typePaginator: Paginator;
  @ViewChild('typeData') typeData: Table;
  @ViewChild('sonPaginator') sonPaginator: Paginator;
  @ViewChild('sonData') sonData: Table;
  display: boolean = false;
  parentDia: boolean = false;
  childDia: boolean = false;
  ngDisplayForm = new QualitySeverityDescDto();
  ngparentForm = new QualityTypeDescDto();
  ngchildForm = new QualityItemInfoDto();
  ccRole: any = [];//不良抄送角色
  unitsList: any = [];//角色列表
  deptList: any = [];//部门列表
  // rolesId: any;//新增二级不良时 保存的 rolesId
  pid: string;//父级选中的id
  saving: boolean = false;//新增不良等级保存按钮  检验节流
  savingNgParentDia: boolean = false;//新增一级不良保存按钮  检验节流
  savingNgChildDia: boolean = false;//新增二级不良保存按钮  检验节流

  constructor(
    injector: Injector,
    private _messageService: MessageService,
    private _qualityManageServiceProxy: QualityManageServiceProxy,
  ) {
    super(injector);
  }

  ngOnInit() {
    this.getRoleNameList()
    // this.getUnitsList()
  }

  //查询 不良严重程度列表
  getSeverityList(event?: LazyLoadEvent): void {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }
    this._qualityManageServiceProxy.getQualitySeverityDescList(
      this.primengTableHelper.getSorting(this.dataTable),//undefined
      this.primengTableHelper.getMaxResultCount(this.paginator, event),//10
      this.primengTableHelper.getSkipCount(this.paginator, event)//0
    )
      .pipe(finalize(() => { }))
      .subscribe(result => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();
      });
  }
  //查询 角色列表
  getRoleNameList(): void {
    this._qualityManageServiceProxy.getRoleManagerNameList()
      .pipe(finalize(() => { }))
      .subscribe(result => {
        this.unitsList = result;
      });

  }
  // 查询 责任部门
  // getUnitsList(): void {
  //   this._qualityManageServiceProxy.getOrganizationUnitsList()
  //     .pipe(finalize(() => { }))
  //     .subscribe(result => {
  //       this.deptList = result;
  //     });

  // }

  //查询 一级不良列表
  getDescList(event?: LazyLoadEvent): void {
    if (this.primengTypeTableHelper.shouldResetPaging(event)) {
      this.typePaginator.changePage(0);
      return;
    }
    this._qualityManageServiceProxy.getQualityTypeDescList(
      this.primengTypeTableHelper.getSorting(this.dataTable),//undefined
      this.primengTypeTableHelper.getMaxResultCount(this.typePaginator, event),//10
      this.primengTypeTableHelper.getSkipCount(this.typePaginator, event)//0
    )
      .pipe(finalize(() => { }))
      .subscribe(result => {
        this.primengTypeTableHelper.totalRecordsCount = result.totalCount;
        this.primengTypeTableHelper.records = result.items;
        this.primengTypeTableHelper.hideLoadingIndicator();
        this.pid = this.primengTypeTableHelper.records[0].id
        this.getInfoList(this.pid, event)

      });

  }
  //查询 二级不良列表
  getInfoList(pid: string, event?: LazyLoadEvent): void {
    if (this.primengSonTableHelper.shouldResetPaging(event)) {
      this.sonPaginator.changePage(0);
      return;
    }
    this._qualityManageServiceProxy.getQualityItemInfoList(
      pid,
      this.primengSonTableHelper.getSorting(this.dataTable),//undefined
      this.primengSonTableHelper.getMaxResultCount(this.sonPaginator, event),//10
      this.primengSonTableHelper.getSkipCount(this.sonPaginator, event)//0
    )
      .pipe(finalize(() => { }))
      .subscribe(result => {
        this.primengSonTableHelper.totalRecordsCount = result.totalCount;
        this.primengSonTableHelper.records = result.items;
        this.primengSonTableHelper.hideLoadingIndicator();
      });

  }
  //新增  不良严重等级
  addSeverityDesc(): void {
    let ccRole = []
    this.ngDisplayForm.ccRole = []
    this.saving = true
    this.ccRole.forEach(element => {
      ccRole.push(Number(element.id))
    });
    this.ngDisplayForm.ccRole = ccRole
    this.ngDisplayForm.isUsed = this.ngDisplayForm.isUsed ? 1 : 0
    this._qualityManageServiceProxy.createOrUpdateQualitySeverityDesc(
      this.ngDisplayForm
    )
      .pipe(finalize(() => { this.saving = false; }))
      .subscribe(result => {
        this.getSeverityList()
        this.display = false
        this._messageService.success('添加成功');

      });
  }
  //新增弹窗隐藏
  displayclose(): void {
    this.ngDisplayForm = new QualitySeverityDescDto();
    this.ngparentForm = new QualityTypeDescDto();
    this.ngchildForm = new QualityItemInfoDto();
    this.ccRole = []
    // this.rolesId = null
  }
  //新增 一级不良
  addLevelDesc(): void {

    this.savingNgParentDia = true
    this.ngparentForm.isUsed = this.ngparentForm.isUsed ? 1 : 0
    this._qualityManageServiceProxy.createOrUpdateQualityTypeDesc(
      this.ngparentForm
    )
      .pipe(finalize(() => { this.savingNgParentDia = false }))
      .subscribe(result => {
        this.getDescList()
        this.parentDia = false
        this._messageService.success('添加成功');

      });
  }
  //新增 二级不良
  addInspectionDesc(): void {
    this.savingNgChildDia = true
    this.ngchildForm.isUsed = this.ngchildForm.isUsed ? 1 : 0
    // this.ngchildForm.organizationUnitsId = this.organizationUnitsId.id
    // this.ngchildForm.rolesId = this.rolesId.id
    this.ngchildForm.typeId = this.pid
    this._qualityManageServiceProxy.createOrUpdateCreateQualityItemInfo(
      this.ngchildForm
    )
      .pipe(finalize(() => { this.savingNgChildDia = false }))
      .subscribe(result => {
        this.getInfoList(this.pid)
        this.childDia = false
        this._messageService.success('添加成功');

      });
  }
  //点击父级更换子级列表数据
  changeChild(index): void {
    this.pid = this.typeData.value[index].id
    this.getInfoList(this.pid)
  }
  //修改 不良严重等级 数据
  updateDataTable(index): void {
    this.ngDisplayForm = this.dataTable.value[index]
    this.display = true
    this.ccRole = []
    let itemCcRole = this.dataTable.value[index].ccRole.split(",")
    itemCcRole.forEach(cI => {
      this.unitsList.forEach(uI => {
        if (cI == uI.displayName) {
          this.ccRole.push(uI)
        }

      });
    });

  }
  //修改 一级不良 数据
  updateLevelTable(index): void {
    this.ngparentForm = this.typeData.value[index]
    this.parentDia = true
  }
  //修改 二级不良 数据
  updateChildTable(index): void {
    this.ngchildForm = this.sonData.value[index]
    this.childDia = true
    // this.deptList.forEach(uI => {
    //   if (this.sonData.value[index].rolesId == uI.id) {
    //     this.rolesId = uI
    //   }

    // });


  }
  // 删除事件
  deleteEv(id, type): void {
    this._messageService.confirm(
      '是否删除这条数据吗？',
      (isConfirmed) => {
        if (isConfirmed) {
          // deleteQualitySeverityDesc 不良严重
          // deleteQualityTypeDesc 一级
          //deleteQualityItemInfo 二级
          if (type == 'deleteQualitySeverityDesc') {
            this._qualityManageServiceProxy.deleteQualitySeverityDesc(
              id
            )
              .pipe(finalize(() => { }))
              .subscribe(result => {
                this.getSeverityList()
                this._messageService.success('删除成功');

              });
          } else if (type == 'deleteQualityTypeDesc') {
            this._qualityManageServiceProxy.deleteQualityTypeDesc(
              id
            )
              .pipe(finalize(() => { }))
              .subscribe(result => {
                this.getDescList()
                this._messageService.success('删除成功');

              });
          } else if (type == 'deleteQualityItemInfo') {
            this._qualityManageServiceProxy.deleteQualityItemInfo(
              id
            )
              .pipe(finalize(() => { }))
              .subscribe(result => {
                this.getInfoList(this.pid)
                this._messageService.success('删除成功');

              });
          }
        }
      }
    );
  }



}
